//-----------------------------------------------------------------------------
// File: ChartScrollerMouseHandler.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System;

namespace ObjectiveDraw.Platform
{
	/// <summary>
	/// Summary description for ChartScrollerMouseHandler.
	/// </summary>
	public class ChartScrollerMouseHandler : ChartMouseHandlerBase 
	{
		public const string SystemComponentId = "ObjectiveDraw.ChartScrollerMouseHandler";

		public ChartScrollerMouseHandler()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		protected float m_DownOrdinateValue;
		protected float m_DownAbscissaValue;
		protected bool m_ShapeDragging;

		public override void OnMouseDown(ObjectiveDraw.Platform.DrawerChartBase control, System.Windows.Forms.MouseEventArgs e)
		{
			DrawerChartBase chart = control as DrawerChartBase;

			float dist = 100;
			IDrawerShape sh = null;
			foreach(IDrawerShape shape in chart.Shapes)
			{
				float d1 = shape.DistanceFrom(control, new System.Drawing.PointF(e.X + control.SurfaceRectangle.X, e.Y + control.SurfaceRectangle.Y));
				if(dist > d1) 
				{
					dist = d1;
					sh = shape;
				}
			}
			
			ObjectiveDraw.Platform.Application _ta = this.DrawerApplication;
			IDrawerShape _shp = _ta.SelectedShape;
			if(_shp!=null)
				_shp.Selected = false;
			_ta.SelectedShape = null;
			
			if(sh!=null && dist < 10)
			{
				if((System.Windows.Forms.Control.ModifierKeys & System.Windows.Forms.Keys.Control) != 0) 
				{
					ICloneable cc = (sh as System.ICloneable);
					if(cc!=null) sh = cc.Clone() as IDrawerShape;
					chart.Shapes.Add(sh);
					sh.Disposing += new EventHandler(chart.OnShapeDisposing);
				}
				sh.Selected = true;
				_ta.SelectedShape = sh;
				sh.OnMouseDown(control, e);
				chart.Invalidate();
				m_ShapeDragging = true;
			}
			else
			{
				DrawerChartContext context = control.Context;

				m_ShapeDragging = false;
				m_DownOrdinateValue = context.OrdinatePosition.Value;
				m_DownAbscissaValue = context.AbscissaPosition.Value;
				base.OnMouseDown (control, e);
			}
		}

		public override void OnMouseMove(ObjectiveDraw.Platform.DrawerChartBase control, System.Windows.Forms.MouseEventArgs e)
		{
			if(m_MouseIsDown && !m_ShapeDragging)
			{
				DrawerChartContext context = control.Context;
				
				if(!control.AutoScale)
				{
					this.OrdinateMouseMove (control, context.OrdinatePosition, e);
				}

				this.AbscissaMouseMove (control, context.AbscissaPosition, e);
			}
			base.OnMouseMove (control, e);
			if(m_ShapeDragging)
			{
				IDrawerShape shp = this.DrawerApplication.SelectedShape;
				if(shp!=null)
					shp.OnMouseMove(control, e);
			}
		}

		public override void OnMouseUp(ObjectiveDraw.Platform.DrawerChartBase control, System.Windows.Forms.MouseEventArgs e)
		{
			if(m_ShapeDragging)
			{
				m_ShapeDragging = false;
				bool CaptureMouse = false;
				IDrawerShape shp = this.DrawerApplication.SelectedShape;
				if(shp!=null)
					shp.OnMouseUp(control, e, ref CaptureMouse);
			}
			base.OnMouseUp (control, e);
		}

		public override void OnKeyDown(DrawerChartBase control, System.Windows.Forms.KeyEventArgs e)
		{
			base.OnKeyDown (control, e);
			IDrawerShape shp = this.DrawerApplication.SelectedShape;
			if(shp!=null)
				shp.OnKeyDown(control, e);
		}

		public override void OnKeyPress(DrawerChartBase control, System.Windows.Forms.KeyPressEventArgs e)
		{
			base.OnKeyPress (control, e);
			IDrawerShape shp = this.DrawerApplication.SelectedShape;
			if(shp!=null)
				shp.OnKeyPress(control, e);
		}

		/*--protected override void OnExecuteInvoke(object sender, Cerebrum.Integrator.InvokeCommandEventArgs e)
		{
			Cerebrum.Integrator.UiCommand command = e.Command;
			if(command.CommandId=="delete")
			{
				ObjectiveDraw.Platform.Workspace _wrk = ObjectiveDraw.Platform.Workspace.FromComponent(this);
				if(_wrk.SelectedShape!=null)
				{
					_wrk.SystemContext.DelSystemInstance(_wrk.SelectedShape.InstanceId, new TradingPlatform.SerializationContext(_wrk.SelectedShape.Master, TradingPlatform.SerializationContextMotive.Destroying));
#warning "Instead of Relaxable.Clean"
					_wrk.SelectedShape = null;
					//_wrk.SelectedShape.Clean(null, null);
					e.Cancel = true;
				}
				return;
			}
			base.OnExecuteInvoke(sender, e);
		}--*/


		
		public void DateTimeMouseDown(Cerebrum.Direct3D.Forms.Control control, CoordinatePosition AbscissaPosition, System.Windows.Forms.MouseEventArgs e)
		{
			m_DownAbscissaValue = AbscissaPosition.Value;
			m_DownMousePosition.X = e.X;
			m_DownMousePosition.Y = e.Y;
			m_MouseIsDown = true;
		}
		public void AbscissaMouseMove(Cerebrum.Direct3D.Forms.Control control, CoordinatePosition AbscissaPosition, System.Windows.Forms.MouseEventArgs e)
		{
			if(m_MouseIsDown)
			{
				int x = e.X - m_DownMousePosition.X;

				AbscissaPosition.Value = (float)(m_DownAbscissaValue - x / AbscissaPosition.Scale);
			}
		}

		public void DateTimeMouseUp(Cerebrum.Direct3D.Forms.Control control, CoordinatePosition AbscissaPosition, System.Windows.Forms.MouseEventArgs e)
		{
			m_MouseIsDown = false;
		}

		
		public void OrdinateMouseDown(Cerebrum.Direct3D.Forms.Control control, CoordinatePosition CoordinatePosition, System.Windows.Forms.MouseEventArgs e)
		{
			m_DownOrdinateValue = CoordinatePosition.Value;
			m_DownMousePosition.X = e.X;
			m_DownMousePosition.Y = e.Y;
			m_MouseIsDown = true;
		}

		public void OrdinateMouseMove(Cerebrum.Direct3D.Forms.Control control, CoordinatePosition CoordinatePosition, System.Windows.Forms.MouseEventArgs e)
		{
			if(m_MouseIsDown)
			{
				int y = e.Y - m_DownMousePosition.Y;

				CoordinatePosition.Value = (float)(m_DownOrdinateValue + y / CoordinatePosition.Scale);
			}
		}

		public void OrdinateMouseUp(Cerebrum.Direct3D.Forms.Control control, CoordinatePosition CoordinatePosition, System.Windows.Forms.MouseEventArgs e)
		{
			m_MouseIsDown = false;
		}
	}
}
